// pages/mineRecord/mineRecord.js
const app = getApp()
var util =  require('../../utils/util.js');
var btnFlag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor: app.globalData.themeColor,
    riceType: 1,
    dateType: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      date: util.setTime(new Date())
    })
  },

  //获取数据
  getData:function(){
    var that = this
    let data = {
      date: that.data.date,
      user_id: that.data.userId,
      date_type: that.data.dateType
    }
    data = JSON.stringify(data)
    util.getAjax(app.globalData.apiUrl +'/Api/Food/get_user_food_order',function(res){
      // console.log(res)
      if(res.data != 0){
        that.setData({
          isReserve: true,
          reserveTxt: that.getType(res.data.type),
          mainType: res.data.type,
          type: res.data.type,
          riceMainType: res.data.rice_type,
          riceType: res.data.rice_type,          
          id: res.data.id
        })
      }else{
        that.setData({
          isReserve: false,
        })
      }
    },{pro: data})
  },

  //获取历史数据
  getList:function(){
    var that = this
    util.getAjax(app.globalData.apiUrl +'/Api/Food/get_user_food_order_list',function(res){
      console.log(res)
      if(res.data.length != 0){
        for(var i in res.data){
          res.data[i].date = res.data[i].date.replace(/\//g,'-')
          res.data[i].week = that.getWeek(new Date(res.data[i].date).getDay())
          res.data[i].value = that.getType2(res.data[i].type)
        }
        that.setData({
          isHistory: true,
          historyList: res.data
        })
      }
    },{user_id: that.data.userId})
  },

  //获取星期
  getWeek:function(type){
    var text = ''
    switch (type) {
      case 0:
        text = '周日'
        break;
      case 1:
        text = '周一'
        break;
     case 2:
        text = '周二'
        break;
      case 3:
        text = '周三'
        break;
      case 4:
        text = '周四'
        break;
      case 5:
        text = '周五'
        break;
      case 6:
        text = '周六'
        break;
    }
    return text
  },

  //获取状态
  getType:function(type){
    var text =''
    var dateType = this.data.dateType == 1?'午餐':'晚餐'
    var note = '已预订：'
    switch (type) {
      case '1':
        text = dateType + note + '常规餐'
        break;
      case '2':
        text = dateType + note + '清真餐'
        break;
      case '3':
        text = dateType + note + '全素餐'
        break;
    }
    return text
  },

  

  //获取状态
  getType2:function(type){
    var text =''
    switch (type) {
      case '1':
        text = '常规餐'
        break;
      case '2':
        text = '清真餐'
        break;
      case '3':
        text = '全素餐'
        break;
    }
    return text
  },

  //获取状态
  getRiceType:function(type){
    var text =''
    switch (type) {
      case '1':
        text = '需要主食'
        break;
      case '2':
        text = '不需要主食'
        break;
    }
    return text
  },

  radioChange:function(e){
    // console.log(e)
    this.setData({
      riceType: e.detail.value
    })
  },

  //订餐按钮
  OrderMealBtn:function(e){
    var that = this
    var type = e.currentTarget.dataset.type
    that.setData({
      type: type
    })
  },

  //更改按钮
  changeBtn:function(){
    var that = this
    if(btnFlag){
      return
    }
    btnFlag = true
    if(that.data.dateType == 0){
      util.showToast('未在定餐时间，或请重试','none',2000)
      btnFlag = false
      return;
    }
    if(that.data.isReserve){
      if(that.data.mainType == that.data.type && that.data.riceMainType == that.data.riceType){
        util.showModal('提示','已预订，不可重复预订')
        btnFlag = false
        return
      }
      if(that.data.status && that.data.status == 1 && that.data.userId && that.data.contentId){
        let data = {
          date: that.data.date,
          type: that.data.type,
          user_id: that.data.userId,
          rice_type: that.data.riceType,
          content_id: app.globalData.contentId,
          date_type: that.data.dateType
        }
        data = JSON.stringify(data)
        util.postAjax(app.globalData.apiUrl +'/Api/Food/yes_food',function(res){
          // console.log(res)
          if(res.data == '1'){
            //成功
            that.setData({
              isReserve: true
            })
            let text = '您' + that.getType(that.data.type) + '并' + that.getRiceType(that.data.riceType) + '成功'
            util.showToast(text,'none',2000)
            that.getData();
            that.getList();
          }else{
            //失败
            util.showModal('提示','订餐失败，请重试')
          }
          btnFlag = false
        },{id: that.data.id,date: that.data.date,pro: data})
      }else{
        wx.login({
          success:function(login){
            // 发送请求
            wx.request({
              url: app.globalData.apiUrl + '/Api/GetUser/get_openid', //接口地址
              data: {
                code: login.code,
              },
              method: 'get',
              header: {
                'content-type': 'application/json' //默认值
              },
              success: function (res) {
                // console.log(res)
                //判断用户是否存在
                if(res.data.code == '1'){
                  //后台有数据
                  app.globalData.userId = res.data.data.id;
                  app.globalData.contentId = res.data.data.content_id;
                  app.globalData.status = res.data.data.status;
                  if(res.data.data.is_manager){
                    app.globalData.isManager = res.data.data.is_manager
                  }
                  if(res.data.data.status == 1){
                    let data = {
                      date: that.data.date,
                      type: type,
                      user_id: res.data.data.id,
                      rice_type: that.data.riceType,
                      content_id: res.data.data.content_id,
                      date_type: that.data.dateType
                    }
                    data = JSON.stringify(data)
                    util.postAjax(app.globalData.apiUrl +'/Api/Food/yes_food',function(res){
                      // console.log(res)
                      if(res.data == '1'){
                        //成功
                        that.setData({
                          isReserve: true
                        })
                        let text = '您' + that.getType(that.data.type) +  '并' + that.getRiceType(that.data.riceType)  + '成功'
                        util.showToast(text,'none',2000)
                        that.getData();
                        that.getList();
                      }else{
                        //失败
                        util.showModal('提示','订餐失败，请重试')
                      }
                      btnFlag = false
                    },{id: that.data.id,date: that.data.date,pro: data})
                  }
                }
              }
            })
          }
        })
      }
    }
  },

  //点击
  clickBtn:function(){
    wx.switchTab({
      url: '../index/index',
    })
  },

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if(app.globalData.mainTime.length != 0){
      let dateType = 0
      for(let i in app.globalData.mainTime){
        let isDate = util.setDate(app.globalData.mainTime[i].food_time,app.globalData.mainTime[i].food_end_time)
        if(isDate){
          dateType = Number(app.globalData.mainTime[i].id)
          break;
        }
      }
      that.setData({
        mainTime: app.globalData.mainTime,
        dateType: Number(dateType)
      })
      
    }else{
      app.mainTimeCallback = mainTime => {
        if (mainTime != '') {
          let dateType = 0
          for(let i in mainTime){
            let isDate = util.setDate(mainTime[i].food_time,mainTime[i].food_end_time)
            if(isDate){
              dateType = Number(mainTime[i].id)
              break;
            }
          }
          that.setData({
            mainTime: mainTime,
            dateType: Number(dateType)
          })
        }
      }
    }
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
      that.getList();
      that.getData();
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          });
          that.getList();
          that.getData();
        }
      }
    }
    if(app.globalData.contentId){
      that.setData({
        contentId: app.globalData.contentId
      })
    }else{
      app.contentIdCallback = contentId => {
        if (contentId != '') {
          that.setData({
            contentId: contentId
          });
        }
      }
    }
    if(app.globalData.status){
      that.setData({
        status: app.globalData.status
      })
    }else{
      app.statusCallback = status => {
        if (status != '') {
          that.setData({
            status: status
          });
        }
      }
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})